package com.train.servlet;

import com.alibaba.fastjson.JSONObject;
import com.train.service.MovieServiceImpI;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.List;

@WebServlet("/movie")
public class MovieServlet extends HttpServlet {
    private MovieServiceImpI movieServiceImpI= new MovieServiceImpI();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取JSON对象
        JSONObject jsonObject = new JSONObject();

        List movieOrder = movieServiceImpI.getMovieOrder();
        if(movieOrder.size()!=0){
            jsonObject.put("code",200);
            jsonObject.put("msg","OK");
            jsonObject.put("data",movieOrder);
        }else{
            jsonObject.put("code",300);
            jsonObject.put("msg","error");
        }
//        response.setContentType("text/html;charset=utf8");
        //把数据响应给前端
        response.getWriter().write(jsonObject.toString());
    }
}
